<?php
namespace app\admin\controller;
use app\model\Msg as MsgModel;
use think\Db;


class Msg extends Base
{
    public function __construct(){
        $model = new MsgModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }


    public function assignSearchs(){
        $model = $this->model;
        $users = Db::name('user')->select();
        $users = arrayParamExtract($users,'id','nickname');

        $assistants = Db::name('assistant')->select();
        $assistants = arrayParamExtract($assistants,'id','name');

        $search_params = [
            [
                ['key'=>'user_id','name'=>'用户','type'=>'select1_1','params'=>$users],
                ['key'=>'assistant_id','name'=>'AI助理','type'=>'select1_1','params'=>$assistants],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'user_id','type'=>'='],
            ['key'=>'assistant_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['user'] = Db::name('user')->field('nickname')->where('id',$vo['user_id'])->find();
            $list['data'][$k]['assistant'] = Db::name('assistant')->field('name')->where('id',$vo['assistant_id'])->find();
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function export(){
        $model = $this->model;

        // 获取筛选
        $map_params = [
            ['key'=>'user_id','type'=>'='],
            ['key'=>'assistant_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        $list = $model->where($map)->order('id','asc')->select();
        $list = $list->toarray();
        foreach ($list as $k => $vo) {
            
        }

        $users = Db::name('user')->select();
        $users = arrayParamExtract($users,'id','nickname');

        $assistants = Db::name('assistant')->select();
        $assistants = arrayParamExtract($assistants,'id','name');

        $data = [];
        // $data[] = ['用户昵称', '用户消息', '发送时间','AI助理','AI答复','答复时间','耗时（s）'];
        $title = ['用户昵称', '用户消息', '发送时间','AI助理','AI答复','答复时间','耗时（s）'];
        foreach ($list as $k => $vo) {
            $tmp = [
                $users[$vo['user_id']],$vo['message'],timeDef($vo['ctime']),
                $assistants[$vo['assistant_id']],$vo['reply'],timeDef($vo['rtime']),
                $vo['duration']
            ];

            foreach ($tmp as $t_k => $t_v) {
                $tmp[$t_k] = str_replace([',',"\n","\r"], '，', $t_v);
            }
            $data[] = $tmp;
        }

        exportCsv(date('YmdHis'),$title,$data);
        // $result = downloadFile(uuidCreate().'.xlsx',$data);
        // if ($result['status'] == 0) {
        //     $this->error($result['msg']);
        // }
    }




   
}
